package com.lulalei.dbviewer.commons.config;

import com.lulalei.dbviewer.commons.bean.DBParameter;
import com.lulalei.dbviewer.commons.exception.CustomizeExceptionHandle;
import com.lulalei.dbviewer.commons.factory.GenerateBeanFactory.OtherModuleGenerateBeanFactory;
import com.lulalei.dbviewer.commons.factory.GenerateBeanFactory.OwnModuleGenerateBeanFactory;
import com.lulalei.dbviewer.commons.factory.GenerateCodeFactory.VelocityGenerateFactory;
import com.lulalei.dbviewer.commons.utils.SpringUtils;
import com.lulalei.dbviewer.service.impl.ViewerServiceImpl;
import com.lulalei.dbviewer.strategy.DBStrategyManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p>
 * </p>
 *
 * @author lulalei
 * @create 2021-05-29 12:35
 */
@Configuration//开启配置
@EnableConfigurationProperties(DBParameter.class)//开启使用映射实体对象
@ConditionalOnClass({ViewerServiceImpl.class, SpringUtils.class})//存在DatabaseServiceImpl时初始化该配置类
@AutoConfigureAfter({DBBasicAutoConfiguration.class})
@ConditionalOnProperty//存在对应配置信息时初始化该配置类
        (
                prefix = "spring.datasource",//存在配置前缀spring.datasource
                value = "enabled",//开启
                matchIfMissing = true//缺失检查
        )
public class DBAutoConfiguration {


    @Bean
    @ConditionalOnMissingBean
    public ViewerServiceImpl viewerService() {
        return new ViewerServiceImpl();
    }

    @Bean
    @ConditionalOnMissingBean
    public SpringUtils springUtils() {
        return new SpringUtils();
    }


    /**
     * 工厂类的注入
     */
    @Configuration
    @ConditionalOnClass({VelocityGenerateFactory.class})
    public static class GenerateFactoryAutoConfiguration {

        @Bean
        @ConditionalOnMissingBean
        public VelocityGenerateFactory velocityGenerateFactory() {
            return new VelocityGenerateFactory();
        }
    }

    @Configuration
    @ConditionalOnClass({OwnModuleGenerateBeanFactory.class, OtherModuleGenerateBeanFactory.class})
    public static class CreateGenerateBeanFactoryAutoConfiguration{
        @Autowired
        DBStrategyManager dbManager;

        @Bean
        @ConditionalOnMissingBean
        public OwnModuleGenerateBeanFactory singleModuleGenerateBeanFactory(){
            return new OwnModuleGenerateBeanFactory(dbManager);
        }

        @Bean
        @ConditionalOnMissingBean
        public OtherModuleGenerateBeanFactory multiModuleGenerateBeanFactory(){
            return new OtherModuleGenerateBeanFactory(dbManager);
        }
    }

    @Configuration
    @ConditionalOnClass({CustomizeExceptionHandle.class})
    public static class ExceptionHandleAutoConfiguration{

        @Bean
        @ConditionalOnMissingBean
        public CustomizeExceptionHandle customizeExceptionHandle(){
            return new CustomizeExceptionHandle();
        }
    }







}
